<?php
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/miniaturephoto/MiniaturePhotoService.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/albumphoto/AlbumPhotoService.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/persistence/photo/PhotoDAO.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/photo/PhotoEntite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/photo/PhotoUtils.php';
PropertiesLoader::loadProperties($_SERVER["DOCUMENT_ROOT"].'/properties.ini');

class PhotoService {
		
		private function PhotoService() {
			//évite la construction
		}
		
		public static function createEnregistrementPhoto($idAlbumPhoto, $nomPhotoAlbum, $commentaire) {
			$photoEntite = new PhotoEntite();
			$photoEntite->nomFichier = $nomPhotoAlbum;
			$photoEntite->commentaire = $commentaire;
			$albumPhotoEntite = AlbumPhotoService::getAlbumPhotoByID($idAlbumPhoto);
			$photoEntite->albumPhoto = $albumPhotoEntite;
			return PhotoDAO::save($photoEntite);
		}
		
		public static function deleteEnregistrementPhoto($idAlbumPhoto, $nomPhotoAlbum) {
			return PhotoDAO::deleteFromNomFichier($idAlbumPhoto, $nomPhotoAlbum);
		}
		
		public static function getPhotoByNomFichierAndIDAlbumPhoto($idAlbumPhoto, $nomPhotoAlbum) {
			return PhotoDAO::getPhotoByNomFichierAndIDAlbumPhoto($idAlbumPhoto, $nomPhotoAlbum);
		}
		
		public static function deletePhoto($albumPhoto, $nomPhotoAlbum) {
			if ($albumPhoto->couverture!= null && $albumPhoto->couverture->nomFichier = $nomPhotoAlbum) {
				//on est en train de supprimer la couverture
				$albumPhoto->couverture = null;
			}
			
			$photoEntite = PhotoDAO::getPhotoByNomFichierAndIDAlbumPhoto($albumPhoto->id, $nomPhotoAlbum);
			
			//supprime le fichier associé
			$nomCompletPhoto = PhotoUtils::getNomCompletPhoto($photoEntite);
			unlink($_SERVER["DOCUMENT_ROOT"].$nomCompletPhoto);
			
			return PhotoDAO::delete($photoEntite->id);
		}
		
		public static function saveCommentaire($idPhoto, $commentaire) {
			$photoEntite = PhotoDAO::getPhotoByID($idPhoto);
			$photoEntite->commentaire = $commentaire;
			PhotoDAO::save($photoEntite);
		}
}
?>